package com.example.config;

import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.ContentTypeDelegatingMessageConverter;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Configuration
public class SerializableConfig {
    @Resource
    private ConfirmCallBackImpl confirmCallBackImpl;
    @Resource
    private ReturnsCallBackImpl returnsCallBackImpl;

    /**
     * 传输序列化对象
     *
     * @return
     */
    @Bean
    public MessageConverter messageConverter() {
        Jackson2JsonMessageConverter jsonMessageConverter = new Jackson2JsonMessageConverter();
        ContentTypeDelegatingMessageConverter converter = new ContentTypeDelegatingMessageConverter(jsonMessageConverter);
        return converter;
    }

    /**
     * 传输序列化对象
     *
     * @return
     */
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        //序列化对象
        Jackson2JsonMessageConverter jsonMessageConverter = new Jackson2JsonMessageConverter();
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(jsonMessageConverter);
        //设置确认回调
        rabbitTemplate.setConfirmCallback(confirmCallBackImpl);
        //设置确认回调
        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setReturnsCallback(returnsCallBackImpl);
        return rabbitTemplate;
    }
}
